libname bond 'D:\name your local directory';
libname factor 'D:\name your local directory\Ken French Data Library';
libname MUI 'D:\name your local directory\Macro uncertainty index';
%Include 'D:\name your local directory\Macros.sas';



*********************************************************************************************************************************************
Add macroeconomic uncertainy index (e.g., the level and the 1-, 6-, and 12-month change to the rolling dataset)
1. MUI_1m_d1 is the macroeconomic uncertainty based on monthly change (e.g., t minus t-1), based on the 1-month-ahead forecasted index
2. MUI_1m_d6 is the macroeconomic uncertainty based on the 6-month change (e.g., t minus t-6), based on the 1-month-ahead forecasted index
3. MUI_1m_d12 is the macroeconomic uncertainty based on the 3-month change (e.g., t minus t-12), based on the 1-month-ahead forecasted index
4. MUI_1m is the level of macroeconomic uncertainty, based on the 1-month-ahead forecasted index;
*********************************************************************************************************************************************;

data MUI;
set MUI.MUI_updated;
if yyyymm>=200207;
run;

data factor;
merge bond.MKTBond MUI;
by yyyymm;
if yyyymm>=200208;
run;


*Add all factors;
proc sql;
create table LTBRoll36Factors_24 as select a.*, b.MKTBond, b.RF, b.MUI_1m, b.MUI_1m_d1, b.MUI_1m_d6, MUI_1m_d12
from bond.Ltbroll36_24 as a left join factor as b
on a.yyyymm=b.yyyymm;
quit;


proc sort data=LTBRoll36Factors_24;
by complete_cusip date descending rankdate;
run;

data bond.LTBRoll36Factors_24;
set LTBRoll36Factors_24;
run;


********************************************************************************************************
Estimate BetaUNC
********************************************************************************************************;


%LET IN = LTBRoll36Factors_24;
%LET OUT = LTB5;



** - Defining the Macro Variables;
**   Risk Factors;

data Reg_varname_Mon_Ind; length Vname $50 Vname_suffix $10; input Vname $ Vname_suffix $;
        datalines;
MKTBond _Coef
MUI_1m _Coef
MUI_1m_d1 _Coef
MUI_1m_d6 _Coef
MUI_1m_d12 _Coef
; run;

**   Characteristics (One-month Lagged);
data Reg_varname_Mon_Ind_Chars; length Vname $100 Vname_suffix $20; input Vname $ Vname_suffix $;
        datalines;
        Rating _Coef
        TMaturity _Coef
    Coupon _Coef
    AMOUNTOUT _Coef
        Duration _Coef
        Return _Coef
; run;
** -Input dataset;

data temp1;
set bond.&IN;
AmountOut=AmountOut/1000; *in billions;
if year(date)>=2002;
run;


%GetRegNameVec(dname=Reg_varname_Mon_Ind,Varrange=(1,2),type=1,NVarInd=Partial);  *for factors;
%GetRegNameVec(dname=Reg_varname_Mon_Ind_Chars,Varrange=(1,2),type=2,NVarInd=Partial); *for characteristics;
%RunTsReg(dname=temp1,Xsecvname=complete_cusip,depvname=Return, depvnameXsec=ReturnDate,
rfvname=RF,Regressors=&Vnamelist,Outdname=temp2,AdjR2=0);


data bond.BetaUNC_Level_1m;
set temp2;
rename MUI_1m = BetaUNC_Level_1m; 
keep date complete_cusip MUI_1m;
run;


%GetRegNameVec(dname=Reg_varname_Mon_Ind,Varrange=(1,3),type=1,NVarInd=Partial);  *for factors;
%GetRegNameVec(dname=Reg_varname_Mon_Ind_Chars,Varrange=(1,2),type=2,NVarInd=Partial); *for characteristics;
%RunTsReg(dname=temp1,Xsecvname=complete_cusip,depvname=Return, depvnameXsec=ReturnDate,
rfvname=RF,Regressors=&Vnamelist,Outdname=temp2,AdjR2=0);


data bond.BetaUNC_1m_d1;
set temp2;
rename MUI_1m_d1 = BetaUNC_1m_d1; 
keep date complete_cusip MUI_1m_d1;
run;



%GetRegNameVec(dname=Reg_varname_Mon_Ind,Varrange=(1,4),type=1,NVarInd=Partial);  *for factors;
%GetRegNameVec(dname=Reg_varname_Mon_Ind_Chars,Varrange=(1,2),type=2,NVarInd=Partial); *for characteristics;
%RunTsReg(dname=temp1,Xsecvname=complete_cusip,depvname=Return, depvnameXsec=ReturnDate,
rfvname=RF,Regressors=&Vnamelist,Outdname=temp2,AdjR2=0);


data bond.BetaUNC_1m_d6;
set temp2;
rename MUI_1m_d6 = BetaUNC_1m_d6; 
keep date complete_cusip MUI_1m_d6;
run;



%GetRegNameVec(dname=Reg_varname_Mon_Ind,Varrange=(1,5),type=1,NVarInd=Partial);  *for factors;
%GetRegNameVec(dname=Reg_varname_Mon_Ind_Chars,Varrange=(1,2),type=2,NVarInd=Partial); *for characteristics;
%RunTsReg(dname=temp1,Xsecvname=complete_cusip,depvname=Return, depvnameXsec=ReturnDate,
rfvname=RF,Regressors=&Vnamelist,Outdname=temp2,AdjR2=0);


data bond.BetaUNC_1m_d12;
set temp2;
rename MUI_1m_d12 = BetaUNC_1m_d12; 
keep date complete_cusip MUI_1m_d12;
run;


*Merge all the betaUNC;

proc sql;
create table LTB3_tmp1 as select a.*, b.BetaUNC_1m_d1
from bond.BetaUNC_Level_1m as a left join bond.BetaUNC_1m_d1 as b
on a.complete_cusip=b.complete_cusip and a.date=b.date;
quit;



proc sql;
create table LTB3_tmp2 as select a.*, b.BetaUNC_1m_d6
from LTB3_tmp1 as a left join bond.BetaUNC_1m_d6 as b
on a.complete_cusip=b.complete_cusip and a.date=b.date;
quit;

proc sql;
create table LTB3_tmp3 as select a.*, b.BetaUNC_1m_d12
from LTB3_tmp2 as a left join bond.BetaUNC_1m_d12 as b
on a.complete_cusip=b.complete_cusip and a.date=b.date;
quit;


proc sort data=LTB3_tmp3;
by complete_cusip date;
run;


data bond.&OUT;
set LTB3_tmp3;
run;
